/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var req;

function Ajax() {
    var ajax;

    if (window.ActiveXObject) {
        try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                ajax = false;
            }
        }
    } else if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest;
    } else {
        ajax = false;
    }

    return ajax;
}




function ValidaCpf(link, idfield) {
    if (!atualizado) {
        field = document.getElementById(idfield);
        req = new Ajax();

        link += "&isIE=0&CPF=" + field.value;
        req.open("GET", link, true);
        textcomp = document.getElementById("avisocpf");
        req.onreadystatechange = CallbackAvisoCpf;
        atualizado = true;
        req.send(null);
    }
}
function CallbackAvisoCpf() {

    if (req == null)
        return;
    if (req.readyState == 4) {
        if (req.status == 200) {
            textcomp.innerHTML = "" + req.responseXML.getElementsByTagName("CPF")[0].childNodes[0].nodeValue + "&nbsp;";
            req = null;
        }
        else {
            alert("Problema no retorno de dados XML do servidor: " + req.statusText);
            req = null;
        }
    }
}



function mask(src, mask) {
    var i = src.value.length;
    var saida = mask.substring(0, 1);
    var texto = mask.substring(i);

    if (texto.substring(0, 1) != saida) {
        src.value += texto.substring(0, 1);
    }
}

function ConverteMaiuscula(Campo) {

    Campo.value = Campo.value.toUpperCase();
}


function validaFormAuto(form)
{
    for (i = 0; i < form.length; i++)
    {
        if (form[i].value == "")
        {
            if (form[i].id.length > 0)
            {
                var nome = form[i].id.substring(1, form[i].id.length);
                var ident = form[i].id;
                alert("O campo " + ident + " é obrigatorio.");
                form[i].focus();
                return false;
            }
        }
    }
    return true
}



function validarEmail() {

    var email = document.getElementById("email");
    if (email.value.length !== 0) {
        exp = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d1,3}(\.\d{1,3}){3}\])$/;
        if (exp.test(email.value)) {
            return true;
        } else {
            alert("E-mail Invalido!");
            email.focus();
            return false;
        }
    } else
        return true;
}



//####################################################################//
// Funcoes abaixo sao usadas para controle de SELLOOKUPCOMBO/TEXTLOOKUP
//####################################################################//
var req = null;
var isIE;
var atualizado = true;
var lookupcomp, textcomp;

var req = null;
var isIE;
var atualizado = true;
var lookupcomp, textcomp;

function RefreshLookup(link,codlookup, idfield) {

    if (!atualizado) {
        codlookupcombo = document.getElementById(codlookup);
        lookupcomp = document.getElementById(idfield);
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
            isIE = false;
            link += "&isIE=0";
        }
        else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            isIE = true;
            link += "&isIE=1";
        }
        if (codlookupcombo.value != "") {

            link += "&codlookup=" + codlookupcombo.value;
//alert(link);				
            req.open("GET", link, true);
            req.onreadystatechange = CallbackLookup;
            req.send(null);
        }

    }
}




function CallbackLookup() {
    if (req == null)
        return;
    if (req.readyState == 4) {
        if (req.status == 200) {
            ClearLookup();
            BuildLookup();
            req = null;
        }
        else {
            alert("Problema no retorno de dados XML do servidor: " + req.statusText);
            req = null;
        }
    }
}
function ClearLookup() {
    while (lookupcomp.length > 0) {
        lookupcomp.remove(0);
    }
}
function BuildLookup() {
    var i = 0, j = 0, k;
    var s, cod, desc;
    var message = req.responseXML.getElementsByTagName("option")[0].childNodes[0].nodeValue;
    AppendLookup(lookupcomp, -1, document.createTextNode("-----"));
    while (message != "") {
        j = message.indexOf(";");
        s = message.substring(0, j);
        if (j == message.length)
            message = "";
        else
            message = message.substring(j + 1);
        k = s.indexOf("-");
        desc = s.substring(0, k);
        cod = s.substring(k + 1, s.length);
        AppendLookup(lookupcomp, cod, document.createTextNode(desc));
    }
    atualizado = true;
}


function AppendLookup(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}







